<%@page import="java.sql.SQLException"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="DAO.DAO_Student"%>
<%
    int totalPages = 0;
    int pageNo = 1;
    DAO_Student dao = new DAO_Student();
    ResultSet rs = null;
    
    String option = request.getParameter("option");
    if (option == null)
        option = "";
    
    String input = request.getParameter("input");
    if (input == null)
        input = "";
    input = new String(input.getBytes("ISO8859_1"), "UTF-8");
    
    if (request.getParameter("search") == null) {
            String paging = request.getParameter("page");
            
            if(paging == null) {
                pageNo = 1;
            }else{
                try{
                    pageNo = Integer.parseInt(paging);
                }catch(NumberFormatException e) {
                    pageNo = 1;
                }
            }
            
            int totalItems = dao.getNumberOfStudent();
            int itemsPerPage = 10;
            
            totalPages = (int) Math.ceil((double)totalItems/itemsPerPage);
            
            if(pageNo <= totalPages) {
                int fro = (pageNo - 1) * 10 + 1;
                String squieo = "SELECT * FROM Student ORDER BY id ASC OFFSET  "+ (fro-1) +" ROWS FETCH NEXT "+ (itemsPerPage) +" ROWS ONLY";
                rs = dao.getData(squieo);
            }else {
                rs = dao.getData("SELECT * FROM Student");
            }
        
    }else
        rs = dao.search(option, input);
%>
<h2 class="sub-header">Students List</h2>
<div class="row">
<div class="pull-right">
<form class="form-inline" role="form" method="post">
  <div class="form-group">
    <div class="input-group">
      <input type="text" name="input" value="<%=input%>" class="form-control" placeholder="Enter input condition">
    </div>
  </div>
    <div class="form-group">
        <select class="form-control" name="option">
          <option <%
            if (option.equalsIgnoreCase("ID"))
                out.print("selected = 'selected'");
          %>>ID</option>
          <option <%
            if (option.equalsIgnoreCase("Name"))
                out.print("selected = 'selected'");
          %>>Name</option>
          <option <%
            if (option.equalsIgnoreCase("Hometown"))
                out.print("selected = 'selected'");
          %>>Hometown</option>
        </select>
    </div>
  <button type="submit" name="search" class="btn btn-default">Search</button>
</form>    
</div>
    </div><div class="row">
<div class="table-responsive">
<table class="table table-striped">
  <thead>
    <tr>
      <th>ID</th>
      <th>Name</th>
      <th>Age</th>
      <th>Sex</th>
      <th>Hometown</th>
    </tr>
  </thead>
  <tbody>
      <%
        while (rs != null && rs.next()) {
            int id = rs.getInt(1);
            String name = rs.getString(2);
            int age = rs.getInt(3);
            
            String sex = "";
            int s = rs.getInt(4);
            if (s == 0)
                sex = "Female";
            else if (s == 1)
                sex = "Male";
            else 
                sex = "Unknown";
            
            String hometown = rs.getString(5);          
      %>
    <tr data-url="<%=id%>" class="student-context-menu">
      <td><%=id%></td>
      <td><%=name%></td>
      <td><%=age%></td>
      <td><%=sex%></td>
      <td><%=hometown%></td>
    </tr>
    <% 
    } %>
  </tbody>
</table>
</div></div>
  <div class="pull-left"><a class="btn btn-primary btn-lg" style="border-radius: 0" href="Student.jsp?action=add"><i class="fa fa-plus-square"></i> Add New Student</a> or <a href="#" data-toggle="modal" data-target="#uploadCSV">upload from CSV file!</a></div>
<nav class="pull-right">
  <ul class="pagination">
    <%
        for(int i = 1; i <= totalPages; i++) {
    %>
    <li <%if(pageNo == i) out.print("class='active'");%>><a href="Student.jsp?page=<%=i%>"><%=i%></a></li>
    <%
        }
    %>
   
  </ul>
</nav>
<div class="modal fade" id="uploadCSV" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
        <h4 class="modal-title" id="myModalLabel">Upload CSV</h4>
      </div>
<form enctype="multipart/form-data" action="UploadFile" method="post">
    

      <div class="modal-body">
          <span class="btn btn-default btn-file">
        Browse <input name="file" type="file"/>
        </span>
          CSV File should follow the format we defined before!
          
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <input type="submit" value="Send File" class="btn btn-primary">
      </div>
</form>            
    </div>
  </div>
</div>    